package com.xiaomai.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.xiaomai.commonutil.R;
import com.xiaomai.servicebase.config.exceptionhandler.GuliException;
import com.xiaomai.vod.service.VodService;
import com.xiaomai.vod.utils.ConstantUtils;
import com.xiaomai.vod.utils.InitVodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/eduvod/video")

public class VodController {

    @Autowired
    private VodService vodService;

    //添加视频
    @PostMapping("updateAlyVideo")
    public R uploadAlyVideo(MultipartFile file){
    String videoId =  vodService.uploadVideoAly(file);
       return R.ok().data("videoId",videoId);
    }

    //删除小节的视频
    @DeleteMapping("removeAlyVideo/{videoId}")
    public R removeAlyVideo(@PathVariable String videoId){
        vodService.removeVideo(videoId);
        return  R.ok();
    }
    //删除课程的视频
    @DeleteMapping("removeCourseAliyunVideos")
    public R removeCourseVideos(@RequestParam("VideoList") List<String> VideoList){
            vodService.removeCourseVideo(VideoList);
            return  R.ok();
    }
    //根据id获取凭证
    @GetMapping("getPlayAuth/{id}")
    public  R getPlayAuth(@PathVariable String id) throws ClientException {

            //创建对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantUtils.ACCESS_KEY_ID, ConstantUtils.ACCESS_KEY_SECRET);
            //创建request和response
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(id);
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
        return R.ok().data("playAuth",playAuth);

    }

}
